<template>
    <div class="EditPoliceRankPopup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
				<el-form-item label="衔称" prop="Name">
				    <el-input v-model="formData.Name" placeholder="请输入衔称" clearable />
				</el-form-item>
				<el-form-item label="授予日期" prop="Date">
                    <el-date-picker v-model="formData.Date" type="date" />
                </el-form-item>
				<el-form-item label="开始日期" prop="EffectiveDateFrom">
                    <el-date-picker v-model="formData.EffectiveDateFrom" type="date" />
                </el-form-item>
				<el-form-item label="结束日期" prop="EffectiveDateEnd">
                    <el-date-picker v-model="formData.EffectiveDateEnd" type="date" />
                </el-form-item>
				<el-form-item label="批准单位" prop="WorkUnit">
                    <el-input v-model="formData.WorkUnit" placeholder="请输入批准单位" clearable />
                </el-form-item>
				<el-form-item label="履历类别" prop="Category">
				    <el-select class="w-[280px]" v-model="formData.Category">
				        <el-option v-for="(item, key) in optionData.RankCategory" :key="key" :label="item.name"
				            :value="item.value" />
				    </el-select>
				</el-form-item>
				<el-form-item label="授衔来源" prop="Source">
                    <el-input v-model="formData.Source" placeholder="请输入授衔来源" clearable />
                </el-form-item>
				<el-form-item label="变动原因" prop="Cause">
                    <el-input v-model="formData.Cause" placeholder="请输入变动原因" clearable />
                </el-form-item>
				<el-form-item label="不保留原因" prop="CauseOfNoRetain">
				    <el-input v-model="formData.CauseOfNoRetain" clearable />
				</el-form-item>
				<el-form-item label="取消原因" prop="CauseOfCancel">
				    <el-input v-model="formData.CauseOfCancel" clearable />
				</el-form-item>
				<el-form-item label="记录状态" prop="LogStatus">
				    <el-input v-model="formData.LogStatus" clearable />
				</el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup name="EditPoliceRankPopup">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { useDictData } from '@/hooks/useDictOptions'
import { EditPoliceRank } from '@/api/consumer'
const { dictData: optionData } = useDictData('RankCategory')
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑警衔信息' : '添加警衔信息'
})
const props = defineProps(['member_id']) /**从view.vue传递参数 */
const formData = reactive({
     id: 0,
     member_id: props.member_id,
     Name: '',
     Date: '',
     EffectiveDateFrom: '',
     EffectiveDateEnd: '',
     WorkUnit: '',
     Category: '',
	 Cause: '',
	 CauseOfRetain: '',
	 CauseOfNoRetain: '',
	 CauseOfCancel: '',
	 LogStatus: '',
     Source: ''
})

const formRules = reactive({
     Name: [
         {
             required: true,
            message: '请输入警衔',
             trigger: ['blur']
         }
     ],
    Date: [
         {
             required: true,
             message: '请输入授予日期',
             trigger: ['blur']
         }
     ],
     EffectiveDateFrom: [
         {
             required: true,
             message: '请输入开始日期',
             trigger: ['blur']
         }
     ],
     WorkUnit: [
         {
             required: true,
             message: '请选择批准单位',
             trigger: ['blur']
         }
     ],
     Category: [
         {
             required: true,
             message: '请选择起始日期',
             trigger: ['blur']
         }
     ]
})


const handleSubmit = async () => {
    await formRef.value?.validate()
    console.log('学历警衔内容', formData)
    await EditPoliceRank(formData)
    //mode.value == 'edit' ? await adminEdit(formData) : await adminAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (row: any) => {
    formData.id = row.id
    const data = await EditPoliceRank({
        id: row.id,
    		 mode:'get'
    })
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            formData[key] = data[key]
        }
    }
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData
})
</script>
